/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.order.service.OrderServiceTest2
   Module Description   :

   Date Created      : 2011/2/1
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.order.service;

import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.apache.commons.lang.xwork.StringUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;

import com.yaodian100.core.common.utils.SpringCommonTest;
import com.yaodian100.ec.order.entity.OrderProduct;
import com.yaodian100.ec.order.entity.OrderProductDetail;
import com.yaodian100.ec.order.entity.Orders;

/**
 * @author feeling.wang
 * 
 */
public class OrderServiceTest_08 extends SpringCommonTest {

	private static OrderService orderService;
	private static JdbcTemplate jdbcTemplate;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			configCtx();
			orderService = ctx.getBean(OrderService.class);
			jdbcTemplate = ctx.getBean(JdbcTemplate.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testUpdOrderData08() throws IOException {
		for (int y = 0; y < 10; y++) {
			List<Map<String, Object>> orders = jdbcTemplate
					.queryForList("select distinct order_id from ( "
							+ "select opd.order_id,o.create_date,o.modify_date,o.modify_user,o.domain_src,opd.order_prod_id,opd.detail_tp,opd.amount,opd.discount,opd.coupon,opd.bonus,opd.giftcard_balance,opd.my_wallet, "
							+ "opd.cash,opd.shipfare,opd.cod_fee,opd.installment_fee,opd.must_pay "
							+ ",opd.amount-opd.discount-opd.coupon-opd.bonus+opd.shipfare+opd.cod_fee+opd.installment_fee result,opd.must_pay+opd.my_wallet+opd.giftcard_balance result2 "
							+ ",o.order_type "
							+ "from ec_order_main o,ec_order_detail opd "
							+ "where  "
							+ "o.order_id = opd.order_id and o.translate_flag = 'Y' "
							+ "and to_char(o.create_date,'yyyyMMdd') between '20110528' and '20110531' "
							+ ")  where result != result2 "
							+ "and order_id not in ( "
							+ "select distinct order_id from ( "
							+ "select od.order_id,o.create_date,o.modify_date,o.modify_user,od.couponid,o.domain_src,od.order_prod_id,od.prod_tp,od.total_price,od.discount,od.coupon,od.bonus,od.giftcard_balance,od.my_wallet, "
							+ "od.cash,od.shipfare,od.cod_fee,od.installment_fee,od.must_pay "
							+ ",od.total_price-od.discount-od.coupon-od.bonus+od.shipfare+od.cod_fee+od.installment_fee result,od.must_pay+od.my_wallet+od.giftcard_balance result2 "
							+ ",o.order_type " + "from ec_order_main o,ec_order_product od "
							+ "where o.order_id = od.order_id and o.translate_flag = 'Y' "
							+ "and to_char(o.create_date,'yyyyMMdd') between '20110528' and '20110531' "
							+ ") where result != result2 ) and rownum < 501 ");
			// .queryForList("select * from ec_order_main where "
			// + "order_id in( '1104207676179')");
			// + "translate_flag is null "
			// +
			// "and order_dt between to_date('2011-12-28 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2011-12-31 23:59:59','yyyy-mm-dd hh24:mi:ss') "
			// + "and rownum < 501 order by order_dt asc ");
			// .queryForList("select order_id from ( "
			// + "select op.order_id,sum(op.cash) cash from ec_order_main o,ec_order_product op "
			// + "where o.order_id = op.order_id and "
			// +
			// "o.cash > 0 and o.order_dt between to_date('2011-06-01','yyyy-MM-dd') and to_date('2011-12-31','yyyy-MM-dd') "
			// + "group by op.order_id " + ") where cash = 0 ");
			String orderId = "";
			Orders order = null;
			for (int k = 0; k < orders.size(); k++) {
				orderId = orders.get(k).get("ORDER_ID").toString();
				System.out.println("orderId:" + orderId);
				synchronized (orderId) {
					try {
						order = orderService.get(orderId);
						if (order.getGiftcardBalance().add(order.getMyWallet()).compareTo(BigDecimal.ZERO) == 1) {
							// 禮品卡 + 帳戶餘額 - order.amount - 积分 > 0 ，才去計算禮品卡、帳戶餘額攤提運費
							BigDecimal paidProd = order.getMyWallet().add(order.getGiftcardBalance()).subtract(
									order.getAmount().subtract(order.getBonus()));
							if (paidProd.compareTo(BigDecimal.ZERO) == 1) {
								// 总共要支付的费用(shipfare+codfee+install-mustpay)
								BigDecimal totalPaidAmount = order.getShipfare().add(order.getCodFee()).add(order.getInstallmentFee())
										.subtract(order.getMustPay());
								if (totalPaidAmount.compareTo(BigDecimal.ZERO) == 1) {
									if (order.getGiftcardBalance().compareTo(totalPaidAmount) == -1) {// 礼品卡金额<支付费用
										if (order.getMyWallet().compareTo(totalPaidAmount) == 1) {// 账户余额<支付费用
											order.setMyWalletShipfare(totalPaidAmount);
										}
									} else {
										order.setGiftcardBalanceShipfare(totalPaidAmount);
									}
								}
							}
						}
						order.setTranslateFlag("Y");
						orderService.update(order);
						// BigDecimal tmpGiftCard = order.getGiftcardBalance().subtract(order.getGiftcardBalanceShipfare());//
						// 礼品卡-礼品卡支付运费
						// BigDecimal tmpMyWallet = order.getMyWallet().subtract(order.getMyWalletShipfare());// 账户余额-账户余额支付运费
						BigDecimal tmpGiftCard = order.getGiftcardBalance();// 礼品卡-礼品卡支付运费
						BigDecimal tmpMyWallet = order.getMyWallet();// 账户余额-账户余额支付运费
						int i = 0, j = 0;
						List<String> opDiscountTypes = Arrays.asList("0", "1", "3", "6", "7");
						List<String> opCouponTypes = Arrays.asList("0", "1", "6", "7");
						BigDecimal remainderProdShipfare = BigDecimal.ZERO;
						BigDecimal remainderProdCodFee = BigDecimal.ZERO;
						BigDecimal remainderProdInstallmentFee = BigDecimal.ZERO;
						BigDecimal remainderProdMustPay = BigDecimal.ZERO;
						BigDecimal remainderProdGiftcardBalance = BigDecimal.ZERO;
						BigDecimal remainderProdMywallet = BigDecimal.ZERO;
						BigDecimal remainderProdBonus = BigDecimal.ZERO;
						BigDecimal remainderDetailShipfare = BigDecimal.ZERO;
						BigDecimal remainderDetailCodFee = BigDecimal.ZERO;
						BigDecimal remainderDetailInstallmentFee = BigDecimal.ZERO;
						BigDecimal remainderDetailMustPay = BigDecimal.ZERO;
						BigDecimal remainderDetailGiftcardBalance = BigDecimal.ZERO;
						BigDecimal remainderDetailMywallet = BigDecimal.ZERO;
						BigDecimal remainderDetailBonus = BigDecimal.ZERO;
						BigDecimal remainderDetailCoupon = BigDecimal.ZERO;
						BigDecimal remainderDetailDiscount = BigDecimal.ZERO;
						BigDecimal orderProdPay = BigDecimal.ZERO;
						BigDecimal orderDetailPay = BigDecimal.ZERO;
						BigDecimal _bonus = BigDecimal.ZERO;
						BigDecimal _shipfare = BigDecimal.ZERO;
						BigDecimal _codfee = BigDecimal.ZERO;
						BigDecimal _installment = BigDecimal.ZERO;
						BigDecimal _giftcard = BigDecimal.ZERO;
						BigDecimal _myWallet = BigDecimal.ZERO;
						BigDecimal _mustPay = BigDecimal.ZERO;
						BigDecimal _discount = BigDecimal.ZERO;
						BigDecimal _coupon = BigDecimal.ZERO;
						BigDecimal orderPay = order.getAmount().subtract(order.getBonus());
						List<OrderProduct> ops = orderProdFilter(order);// 贈品不列入計算,因积分的比例分配问题，避免最后一单的金额很小，却分配到剩余积分，所以依金额由小到大排序
						// 外部商城訂單允許到小數點二位數
						int dot = 1;
						if (StringUtils.isNotBlank(order.getOuterOrderId())) {
							dot = 2;
						}
						for (OrderProduct op : ops) {
							if (i < ops.size() - 1) {
								if (order.getBonus().compareTo(BigDecimal.ZERO) == 1) {
									_bonus = order.getBonus().multiply(op.getTotalPrice().subtract(op.getDiscount())).divide(
											order.getTotalPrice().subtract(order.getDiscount()), dot, BigDecimal.ROUND_CEILING);
									remainderProdBonus = remainderProdBonus.add(_bonus);
									if (remainderProdBonus.compareTo(order.getBonus()) == 1) {
										remainderProdBonus = remainderProdBonus.subtract(_bonus);
										_bonus = order.getBonus().subtract(remainderProdBonus);
										remainderProdBonus = order.getBonus();
									}
									op.setBonus(_bonus);
								} else {
									op.setBonus(BigDecimal.ZERO);
								}
								orderProdPay = op.getAmount().subtract(op.getBonus());
								if (orderProdPay.compareTo(BigDecimal.ZERO) == 0) {
									orderProdPay = new BigDecimal("1");
								}

								if (order.getShipfare().compareTo(BigDecimal.ZERO) == 1) {
									_shipfare = order.getShipfare().multiply(orderProdPay)
											.divide(orderPay, dot, BigDecimal.ROUND_CEILING);
									remainderProdShipfare = remainderProdShipfare.add(_shipfare);
									if (remainderProdShipfare.compareTo(order.getShipfare()) == 1) {
										remainderProdShipfare = remainderProdShipfare.subtract(_shipfare);
										_shipfare = order.getShipfare().subtract(remainderProdShipfare);
										remainderProdShipfare = order.getShipfare();
									}
									op.setShipfare(_shipfare);
								} else {
									op.setShipfare(BigDecimal.ZERO);
								}
								if (order.getCodFee().compareTo(BigDecimal.ZERO) == 1) {
									_codfee = order.getCodFee().multiply(orderProdPay).divide(orderPay, dot, BigDecimal.ROUND_CEILING);
									remainderProdCodFee = remainderProdCodFee.add(_codfee);
									if (remainderProdCodFee.compareTo(order.getCodFee()) == 1) {
										remainderProdCodFee = remainderProdCodFee.subtract(_codfee);
										_codfee = order.getCodFee().subtract(remainderProdCodFee);
										remainderProdCodFee = order.getCodFee();
									}
									op.setCodFee(_codfee);
								} else {
									op.setCodFee(BigDecimal.ZERO);
								}
								if (order.getInstallmentFee().compareTo(BigDecimal.ZERO) == 1) {
									_installment = order.getInstallmentFee().multiply(orderProdPay).divide(orderPay, dot,
											BigDecimal.ROUND_CEILING);
									remainderProdInstallmentFee = remainderProdInstallmentFee.add(_installment);
									if (remainderProdInstallmentFee.compareTo(order.getInstallmentFee()) == 1) {
										remainderProdInstallmentFee = remainderProdInstallmentFee.subtract(_installment);
										_installment = order.getInstallmentFee().subtract(remainderProdInstallmentFee);
										remainderProdInstallmentFee = order.getInstallmentFee();
									}
									op.setInstallmentFee(_installment);
								} else {
									op.setInstallmentFee(BigDecimal.ZERO);
								}

								if (order.getMustPay().compareTo(BigDecimal.ZERO) == 1) {
									if ((order.getMyWallet().compareTo(BigDecimal.ZERO) == 0 && order.getGiftcardBalance().compareTo(
											BigDecimal.ZERO) == 0)
											|| (order.getMyWallet().compareTo(remainderProdMywallet) == 0 && order.getGiftcardBalance()
													.compareTo(remainderProdGiftcardBalance) == 0)) {
										_mustPay = op.getAmount().subtract(op.getBonus()).add(op.getShipfare()).add(op.getCodFee()).add(
												op.getInstallmentFee());
									} else {
										_mustPay = order.getMustPay().multiply(orderProdPay)
												.divide(orderPay, dot, BigDecimal.ROUND_CEILING);
									}
									remainderProdMustPay = remainderProdMustPay.add(_mustPay);
									if (remainderProdMustPay.compareTo(order.getMustPay()) == 1) {
										remainderProdMustPay = remainderProdMustPay.subtract(_mustPay);
										_mustPay = order.getMustPay().subtract(remainderProdMustPay);
										remainderProdMustPay = order.getMustPay();
									}
									op.setMustPay(_mustPay);
								} else {
									op.setMustPay(BigDecimal.ZERO);
								}
								if (order.getCash().compareTo(BigDecimal.ZERO) == 1) {
									op.setCash(op.getMustPay());
								} else {
									op.setCash(BigDecimal.ZERO);
								}

								if (order.getGiftcardBalance().compareTo(BigDecimal.ZERO) == 1
										&& order.getMyWallet().compareTo(BigDecimal.ZERO) == 0) {// 使用礼品卡不使用账户余额支付
									_giftcard = op.getAmount().subtract(op.getBonus()).subtract(op.getMustPay()).add(op.getShipfare())
											.add(op.getCodFee()).add(op.getInstallmentFee());
									remainderProdGiftcardBalance = remainderProdGiftcardBalance.add(_giftcard);
									if (remainderProdGiftcardBalance.compareTo(tmpGiftCard) == 1) {
										remainderProdGiftcardBalance = remainderProdGiftcardBalance.subtract(_giftcard);
										_giftcard = tmpGiftCard.subtract(remainderProdGiftcardBalance);
										remainderProdGiftcardBalance = tmpGiftCard;
									}
									op.setGiftcardBalance(_giftcard);
								} else {
									op.setGiftcardBalance(BigDecimal.ZERO);
								}
								if (order.getGiftcardBalance().compareTo(BigDecimal.ZERO) == 0
										&& order.getMyWallet().compareTo(BigDecimal.ZERO) == 1) {// 使用账户余额不使用礼品卡支付
									_myWallet = op.getAmount().subtract(op.getBonus()).subtract(op.getMustPay()).add(op.getShipfare())
											.add(op.getCodFee()).add(op.getInstallmentFee());
									remainderProdMywallet = remainderProdMywallet.add(_myWallet);
									if (remainderProdMywallet.compareTo(tmpMyWallet) == 1) {
										remainderProdMywallet = remainderProdMywallet.subtract(_myWallet);
										_myWallet = tmpMyWallet.subtract(remainderProdMywallet);
										remainderProdMywallet = tmpMyWallet;
									}
									op.setMyWallet(_myWallet);
								} else {
									op.setMyWallet(BigDecimal.ZERO);
								}
								if (order.getGiftcardBalance().compareTo(BigDecimal.ZERO) == 1
										&& order.getMyWallet().compareTo(BigDecimal.ZERO) == 1) {// 使用礼品卡和账户余额支付
									_myWallet = tmpMyWallet.multiply(orderProdPay).divide(orderPay, dot, BigDecimal.ROUND_CEILING);
									remainderProdMywallet = remainderProdMywallet.add(_myWallet);
									if (remainderProdMywallet.compareTo(tmpMyWallet) == 1) {
										remainderProdMywallet = remainderProdMywallet.subtract(_myWallet);
										_myWallet = tmpMyWallet.subtract(remainderProdMywallet);
										remainderProdMywallet = tmpMyWallet;
									}
									op.setMyWallet(_myWallet);
									_giftcard = op.getAmount().subtract(op.getBonus()).subtract(op.getMustPay()).subtract(
											op.getMyWallet()).add(op.getShipfare()).add(op.getCodFee()).add(op.getInstallmentFee());
									remainderProdGiftcardBalance = remainderProdGiftcardBalance.add(_giftcard);
									if (remainderProdGiftcardBalance.compareTo(tmpGiftCard) == 1) {
										remainderProdGiftcardBalance = remainderProdGiftcardBalance.subtract(_giftcard);
										_giftcard = tmpGiftCard.subtract(remainderProdGiftcardBalance);
										remainderProdGiftcardBalance = tmpGiftCard;
									}
									op.setGiftcardBalance(_giftcard);
								}
							} else {
								op.setGiftcardBalance(tmpGiftCard.subtract(remainderProdGiftcardBalance));
								op.setMyWallet(tmpMyWallet.subtract(remainderProdMywallet));
								op.setBonus(order.getBonus().subtract(remainderProdBonus));
								op.setMustPay(order.getMustPay().subtract(remainderProdMustPay));
								op.setShipfare(order.getShipfare().subtract(remainderProdShipfare));
								op.setCodFee(order.getCodFee().subtract(remainderProdCodFee));
								op.setInstallmentFee(order.getInstallmentFee().subtract(remainderProdInstallmentFee));
								if (order.getCash().compareTo(BigDecimal.ZERO) == 1) {
									op.setCash(op.getMustPay());
								} else {
									op.setCash(BigDecimal.ZERO);
								}
							}
							orderService.saveOrderProd(op);
							i++;
							List<OrderProductDetail> opds = orderProdDetailFilter(op);// 贈品不列入計算
							remainderDetailShipfare = BigDecimal.ZERO;
							remainderDetailCodFee = BigDecimal.ZERO;
							remainderDetailInstallmentFee = BigDecimal.ZERO;
							remainderDetailMustPay = BigDecimal.ZERO;
							remainderDetailGiftcardBalance = BigDecimal.ZERO;
							remainderDetailMywallet = BigDecimal.ZERO;
							remainderDetailBonus = BigDecimal.ZERO;
							remainderDetailCoupon = BigDecimal.ZERO;
							remainderDetailDiscount = BigDecimal.ZERO;
							j = 0;
							orderProdPay = op.getAmount().subtract(op.getBonus());
							if (orderProdPay.compareTo(BigDecimal.ZERO) == 0) {
								orderProdPay = new BigDecimal("1");
							}
							for (OrderProductDetail opd : opds) {
								if (j < opds.size() - 1) {
									if (op.getBonus().compareTo(BigDecimal.ZERO) == 1) {
										_bonus = op.getBonus().multiply(opd.getAmount()).divide(op.getTotalPrice(), dot,
												BigDecimal.ROUND_CEILING);
										remainderDetailBonus = remainderDetailBonus.add(_bonus);
										if (remainderDetailBonus.compareTo(op.getBonus()) == 1) {
											remainderDetailBonus = remainderDetailBonus.subtract(_bonus);
											_bonus = op.getBonus().subtract(remainderDetailBonus);
											remainderDetailBonus = op.getBonus();
										}
										opd.setBonus(_bonus);
									} else {
										opd.setBonus(BigDecimal.ZERO);
									}
									if (opDiscountTypes.contains(opd.getDetailTp())) {
										if (op.getDiscount().compareTo(BigDecimal.ZERO) == 1) {
											_discount = op.getDiscount().multiply(opd.getAmount()).divide(op.getTotalPrice(), dot,
													BigDecimal.ROUND_CEILING);
											remainderDetailDiscount = remainderDetailDiscount.add(_discount);
											if (remainderDetailDiscount.compareTo(op.getDiscount()) == 1) {
												remainderDetailDiscount = remainderDetailDiscount.subtract(_discount);
												_discount = op.getDiscount().subtract(remainderDetailDiscount);
												remainderDetailDiscount = op.getDiscount();
											}
											opd.setDiscount(_discount);
										} else {
											opd.setDiscount(BigDecimal.ZERO);
										}
									} else {
										opd.setDiscount(BigDecimal.ZERO);
									}
									if (opCouponTypes.contains(opd.getDetailTp())) {
										if (op.getCoupon().compareTo(BigDecimal.ZERO) == 1) {
											if (!"6".equals(opd.getDetailTp())) {
												_coupon = op.getCoupon();
											} else {
												_coupon = op.getCoupon().divide(BigDecimal.valueOf(opds.size()), dot, BigDecimal.ROUND_CEILING);
												if (_coupon.compareTo(opd.getAmount().subtract(opd.getDiscount())) == 1) {
													_coupon = opd.getAmount().subtract(opd.getDiscount());
												}
											}
											opd.setCoupon(_coupon);
											remainderDetailCoupon = remainderDetailCoupon.add(_coupon);
										} else {
											opd.setCoupon(BigDecimal.ZERO);
										}
									} else {
										opd.setCoupon(BigDecimal.ZERO);
									}
									orderDetailPay = opd.getAmount().subtract(opd.getBonus()).subtract(opd.getDiscount()).subtract(
											opd.getCoupon());
									if (orderDetailPay.compareTo(BigDecimal.ZERO) == 0) {
										orderDetailPay = new BigDecimal("1");
									}
									if (op.getShipfare().compareTo(BigDecimal.ZERO) == 1) {
										_shipfare = op.getShipfare().multiply(orderDetailPay).divide(orderProdPay, dot,
												BigDecimal.ROUND_CEILING);
										remainderDetailShipfare = remainderDetailShipfare.add(_shipfare);
										if (remainderDetailShipfare.compareTo(op.getShipfare()) == 1) {
											remainderDetailShipfare = remainderDetailShipfare.subtract(_shipfare);
											_shipfare = op.getShipfare().subtract(remainderDetailShipfare);
											remainderDetailShipfare = op.getShipfare();
										}
										opd.setShipfare(_shipfare);
									} else {
										opd.setShipfare(BigDecimal.ZERO);
									}
									if (op.getCodFee().compareTo(BigDecimal.ZERO) == 1) {
										_codfee = op.getCodFee().multiply(orderDetailPay).divide(orderProdPay, dot,
												BigDecimal.ROUND_CEILING);
										remainderDetailCodFee = remainderDetailCodFee.add(_codfee);
										if (remainderDetailCodFee.compareTo(op.getCodFee()) == 1) {
											remainderDetailCodFee = remainderDetailCodFee.subtract(_codfee);
											_codfee = op.getCodFee().subtract(remainderDetailCodFee);
											remainderDetailCodFee = op.getCodFee();
										}
										opd.setCodFee(_codfee);
									} else {
										opd.setCodFee(BigDecimal.ZERO);
									}
									if (op.getInstallmentFee().compareTo(BigDecimal.ZERO) == 1) {
										_installment = op.getInstallmentFee().multiply(orderDetailPay).divide(orderProdPay, dot,
												BigDecimal.ROUND_CEILING);
										remainderDetailInstallmentFee = remainderDetailInstallmentFee.add(_installment);
										if (remainderDetailInstallmentFee.compareTo(op.getInstallmentFee()) == 1) {
											remainderDetailInstallmentFee = remainderDetailInstallmentFee.subtract(_installment);
											_installment = op.getInstallmentFee().subtract(remainderDetailInstallmentFee);
											remainderDetailInstallmentFee = op.getInstallmentFee();
										}
										opd.setInstallmentFee(_installment);
									} else {
										opd.setInstallmentFee(BigDecimal.ZERO);
									}

									if (op.getMustPay().compareTo(BigDecimal.ZERO) == 1) {
										if (op.getMyWallet().compareTo(BigDecimal.ZERO) == 0
												&& op.getGiftcardBalance().compareTo(BigDecimal.ZERO) == 0) {
											_mustPay = opd.getAmount().subtract(opd.getCoupon()).subtract(opd.getDiscount()).subtract(
													opd.getBonus()).add(opd.getShipfare()).add(opd.getCodFee()).add(opd.getInstallmentFee());
										} else {
											_mustPay = op.getMustPay().multiply(orderDetailPay).divide(orderProdPay, dot,
													BigDecimal.ROUND_CEILING);
										}
										remainderDetailMustPay = remainderDetailMustPay.add(_mustPay);
										if (remainderDetailMustPay.compareTo(op.getMustPay()) == 1) {
											remainderDetailMustPay = remainderDetailMustPay.subtract(_mustPay);
											_mustPay = op.getMustPay().subtract(remainderDetailMustPay);
											remainderDetailMustPay = op.getMustPay();
										}
										opd.setMustPay(_mustPay);
									} else {
										opd.setMustPay(BigDecimal.ZERO);
									}
									if (op.getCash().compareTo(BigDecimal.ZERO) == 1) {
										opd.setCash(opd.getMustPay());
									} else {
										opd.setCash(BigDecimal.ZERO);
									}

									if (op.getGiftcardBalance().compareTo(BigDecimal.ZERO) == 1
											&& op.getMyWallet().compareTo(BigDecimal.ZERO) == 0) {// 使用礼品卡不使用账户余额支付
										_giftcard = opd.getAmount().subtract(opd.getCoupon()).subtract(opd.getDiscount()).subtract(
												opd.getBonus()).subtract(opd.getMustPay()).add(opd.getShipfare()).add(opd.getCodFee()).add(
												opd.getInstallmentFee());
										remainderDetailGiftcardBalance = remainderDetailGiftcardBalance.add(_giftcard);
										if (remainderDetailGiftcardBalance.compareTo(op.getGiftcardBalance()) == 1) {
											remainderDetailGiftcardBalance = remainderDetailGiftcardBalance.subtract(_giftcard);
											_giftcard = op.getGiftcardBalance().subtract(remainderDetailGiftcardBalance);
											remainderDetailGiftcardBalance = op.getGiftcardBalance();
										}
										opd.setGiftcardBalance(_giftcard);
									} else {
										opd.setGiftcardBalance(BigDecimal.ZERO);
									}
									if (op.getGiftcardBalance().compareTo(BigDecimal.ZERO) == 0
											&& op.getMyWallet().compareTo(BigDecimal.ZERO) == 1) {// 使用账户余额不使用礼品卡支付
										_myWallet = opd.getAmount().subtract(opd.getCoupon()).subtract(opd.getDiscount()).subtract(
												opd.getBonus()).subtract(opd.getMustPay()).add(opd.getShipfare()).add(opd.getCodFee()).add(
												opd.getInstallmentFee());
										remainderDetailMywallet = remainderDetailMywallet.add(_myWallet);
										if (remainderDetailMywallet.compareTo(op.getMyWallet()) == 1) {
											remainderDetailMywallet = remainderDetailMywallet.subtract(_myWallet);
											_myWallet = op.getMyWallet().subtract(remainderDetailMywallet);
											remainderDetailMywallet = op.getMyWallet();
										}
										opd.setMyWallet(_myWallet);
									} else {
										opd.setMyWallet(BigDecimal.ZERO);
									}
									if (op.getGiftcardBalance().compareTo(BigDecimal.ZERO) == 1
											&& op.getMyWallet().compareTo(BigDecimal.ZERO) == 1) {// 使用礼品卡和账户余额支付
										_myWallet = op.getMyWallet().multiply(orderDetailPay).divide(orderProdPay, dot,
												BigDecimal.ROUND_CEILING);
										remainderDetailMywallet = remainderDetailMywallet.add(_myWallet);
										if (remainderDetailMywallet.compareTo(op.getMyWallet()) == 1) {
											remainderDetailMywallet = remainderDetailMywallet.subtract(_myWallet);
											_myWallet = op.getMyWallet().subtract(remainderDetailMywallet);
											remainderDetailMywallet = op.getMyWallet();
										}
										opd.setMyWallet(_myWallet);
										_giftcard = opd.getAmount().subtract(opd.getCoupon()).subtract(opd.getDiscount()).subtract(
												opd.getBonus()).subtract(opd.getMyWallet()).subtract(opd.getMustPay()).add(opd.getShipfare())
												.add(opd.getCodFee()).add(opd.getInstallmentFee());
										remainderDetailGiftcardBalance = remainderDetailGiftcardBalance.add(_giftcard);
										if (remainderDetailGiftcardBalance.compareTo(op.getGiftcardBalance()) == 1) {
											remainderDetailGiftcardBalance = remainderDetailGiftcardBalance.subtract(_giftcard);
											_giftcard = op.getGiftcardBalance().subtract(remainderDetailGiftcardBalance);
											remainderDetailGiftcardBalance = op.getGiftcardBalance();
										}
										opd.setGiftcardBalance(_giftcard);
									}
								} else {
									opd.setGiftcardBalance(op.getGiftcardBalance().subtract(remainderDetailGiftcardBalance));
									opd.setMyWallet(op.getMyWallet().subtract(remainderDetailMywallet));
									opd.setBonus(op.getBonus().subtract(remainderDetailBonus));
									opd.setMustPay(op.getMustPay().subtract(remainderDetailMustPay));
									opd.setShipfare(op.getShipfare().subtract(remainderDetailShipfare));
									opd.setCodFee(op.getCodFee().subtract(remainderDetailCodFee));
									opd.setInstallmentFee(op.getInstallmentFee().subtract(remainderDetailInstallmentFee));
									if (opDiscountTypes.contains(opd.getDetailTp())) {
										opd.setDiscount(op.getDiscount().subtract(remainderDetailDiscount));
									} else {
										opd.setDiscount(BigDecimal.ZERO);
									}
									if (opCouponTypes.contains(opd.getDetailTp())) {
										opd.setCoupon(op.getCoupon().subtract(remainderDetailCoupon));
									} else {
										opd.setCoupon(BigDecimal.ZERO);
									}
									if (op.getCash().compareTo(BigDecimal.ZERO) == 1) {
										opd.setCash(opd.getMustPay());
									} else {
										opd.setCash(BigDecimal.ZERO);
									}
								}
								orderService.saveOrderDetail(opd);
								j++;
							}
						}
					} catch (Exception e) {
						System.out.println(e.getMessage());
					}
				}
			}
		}
	}

	List<OrderProductDetail> orderProdDetailFilter(OrderProduct orderProduct) {
		List<OrderProductDetail> newOpds = new ArrayList<OrderProductDetail>();
		if (null != orderProduct.getOrderProductDetail() && orderProduct.getOrderProductDetail().size() > 0) {
			int i = 0;
			// 过滤赠品(赠品不分担任何费用)
			for (OrderProductDetail opd : orderProduct.getOrderProductDetail()) {
				if (!"2".equals(opd.getDetailTp())) {
					newOpds.add(i, opd);
					i++;
				}
			}
			if (newOpds.size() > 0) {
				// 依amount排序(递增)
				Collections.sort(newOpds, new Comparator() {
					@Override
					public int compare(Object o1, Object o2) {
						OrderProductDetail opd1 = (OrderProductDetail) o1;
						OrderProductDetail opd2 = (OrderProductDetail) o2;
						return opd1.getAmount().compareTo(opd2.getAmount());
					}
				});
			}
		}
		return newOpds;
	}

	List<OrderProductDetail> orderProdDetailFilter2(OrderProduct orderProduct) {
		List<OrderProductDetail> newOpds = new ArrayList<OrderProductDetail>();
		if (null != orderProduct.getOrderProductDetail() && orderProduct.getOrderProductDetail().size() > 0) {
			int i = 0;
			// 过滤赠品(赠品不分担任何费用)
			for (OrderProductDetail opd : orderProduct.getOrderProductDetail()) {
				if (!Arrays.asList("2", "3").contains(opd.getDetailTp())) {
					newOpds.add(i, opd);
					i++;
				}
			}
			if (newOpds.size() > 0) {
				// 依amount排序(递增)
				Collections.sort(newOpds, new Comparator() {
					@Override
					public int compare(Object o1, Object o2) {
						OrderProductDetail opd1 = (OrderProductDetail) o1;
						OrderProductDetail opd2 = (OrderProductDetail) o2;
						return opd1.getAmount().compareTo(opd2.getAmount());
					}
				});
			}
		}
		return newOpds;
	}

	List<OrderProduct> orderProdFilter(Orders order) {
		List<OrderProduct> newOps = new ArrayList<OrderProduct>();
		if (null != order.getOrderProduct() && order.getOrderProduct().size() > 0) {
			int i = 0;
			// 过滤赠品(赠品不分担任何费用)
			for (OrderProduct op : order.getOrderProduct()) {
				if (!"2".equals(op.getProdTp())) {
					newOps.add(i, op);
					i++;
				}
			}
			if (newOps.size() > 0) {
				// 依amount排序(递增)
				Collections.sort(newOps, new Comparator() {
					@Override
					public int compare(Object o1, Object o2) {
						OrderProduct op1 = (OrderProduct) o1;
						OrderProduct op2 = (OrderProduct) o2;
						return op1.getAmount().compareTo(op2.getAmount());
					}
				});
			}
		}
		return newOps;
	}

	@Test
	public void testXML() {
		Orders order = orderService.getOrder("1112207043351");
		String xml = null;
		try {
			StringWriter writer = new StringWriter();
			JAXBContext context = JAXBContext.newInstance(Orders.class);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // pretty print XML
			marshaller.marshal(order, writer);
			xml = writer.toString();

			// ByteArrayOutputStream baos = new ByteArrayOutputStream();
			// BeanWriter beanWriter = new BeanWriter(baos);
			// beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
			// // beanWriter.getBindingConfiguration().setMapIDs(false);
			// beanWriter.enablePrettyPrint();
			//
			// // beanWriter.enablePrettyPrint();
			// //
			// beanWriter.write(order);
			// xml = baos.toString();
			// beanWriter.flush();
			// baos.flush();
			// beanWriter.close();
			// baos.close();
			System.out.println(xml);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}
}